package com.niiwoo.civet.trade.service.local.third;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dangdang.ddframe.rdb.sharding.api.HintManager;
import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder;
import com.niiwoo.activity.stage.dto.request.prize.*;
import com.niiwoo.activity.stage.dto.response.prize.UserPrizeResponseDTO;
import com.niiwoo.activity.stage.enums.prize.UserPrizeUseSceneEnum;
import com.niiwoo.activity.stage.service.prize.ActivityPrizeDubboService;
import com.niiwoo.civet.trade.dao.entity.*;
import com.niiwoo.civet.trade.dao.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.concurrent.CompletableFuture;

/**
 * Created by yuanguang on 2017/12/22.
 */
@Service
@Slf4j
public class ActivityLocalService {
    @Autowired
    private ProjectMapperExt projectMapperExt;
    @Autowired
    private ProjectBorrowerSummaryMapperExt projectBorrowerSummaryMapperExt;
    @Reference(version = "1.0.0")
    ActivityPrizeDubboService activityPrizeDubboService;

    /**
     * 还款红包返现-借款返现红包
     * 正常还款useSceneEnum可以为空，提前还款必传
     * @param projectId
     * @param useSceneEnum
     */
    public void useLoancashBackPrize(Long projectId,UserPrizeUseSceneEnum useSceneEnum){
        CompletableFuture.runAsync(()->{
            try{
                if (!HintManagerHolder.isMasterRouteOnly()) {
                    HintManagerHolder.clear();
                    HintManager hintManager = HintManager.getInstance();
                    hintManager.setMasterRouteOnly();
                }
                Project project = projectMapperExt.selectByPrimaryKey(projectId);
                Assert.notNull(project,"数据错误，project不可空");
                if(project.getPrizeId()==null || project.getPrizeId().equals(0L)){
                    log.info("调用活动借款返现接口useLoancashBackPrize奖品ID为空直接返回");
                    return;
                }
                log.info("调用活动查询接口getUserPrizeInfo入参，userid={},prizeId={}",project.getBorrowUserId(), project.getPrizeId());
                UserPrizeResponseDTO userPrizeInfo = activityPrizeDubboService.getUserPrizeInfo(project.getBorrowUserId(),project.getPrizeId());
                log.info("调用活动查询接口getUserPrizeInfo返回，userPrizeInfo={}",JSON.toJSONString(userPrizeInfo));
                Assert.notNull(userPrizeInfo,"数据错误，userPrizeInfo不可空");
                //11 借款返现红包
                if(userPrizeInfo.getPrizeTypeId()==11){
                    if (!HintManagerHolder.isMasterRouteOnly()) {
                        HintManagerHolder.clear();
                        HintManager hintManager = HintManager.getInstance();
                        hintManager.setMasterRouteOnly();
                    }
                    ProjectBorrowerSummary summary = projectBorrowerSummaryMapperExt.selectByPrimaryKey(projectId);
                    Assert.notNull(summary, "数据错误，summary不可空");
                    UseLoanCashBackReqDTO requestDTO = new UseLoanCashBackReqDTO();
                    requestDTO.setUserId(project.getBorrowUserId());
                    requestDTO.setPrizeId(project.getPrizeId());
                    requestDTO.setCurrentPeriods(summary.getCurrentPeriod());
                    if (useSceneEnum != null) {
                        requestDTO.setUserPrizeUseSceneEnum(useSceneEnum);
                    }
                    log.info("调用活动借款返现接口useLoancashBackPrize入参，requestDTO={}", JSON.toJSONString(requestDTO));
                    activityPrizeDubboService.useLoancashBackPrize(requestDTO);
                }
            }catch (Exception e){
                log.error("useLoancashBackPrize Exception,",e);
            }
        });

    }

    /**
     * 还款红包返现-借款利率折扣卡
     * 正常还款useSceneEnum可以为空，提前还款必传
     * @param projectId
     * @param currentPeriod
     * @param useSceneEnum
     */
    public void useInterestDiscountPrize(Long projectId,Integer currentPeriod,UserPrizeUseSceneEnum useSceneEnum){
        CompletableFuture.runAsync(()->{
            try{
                Project project = projectMapperExt.selectByPrimaryKey(projectId);
                Assert.notNull(project,"数据错误，project不可空");
                if(project.getPrizeId()==null || project.getPrizeId().equals(0L)){
                    log.info("调用活动借款利率折扣卡接口useInterestDiscountPrize奖品ID为空直接返回");
                    return;
                }
                log.info("调用活动查询接口getUserPrizeInfo入参，userid={},prizeId={}",project.getBorrowUserId(), project.getPrizeId());
                UserPrizeResponseDTO userPrizeInfo = activityPrizeDubboService.getUserPrizeInfo(project.getBorrowUserId(),project.getPrizeId());
                log.info("调用活动查询接口getUserPrizeInfo返回，userPrizeInfo={}",JSON.toJSONString(userPrizeInfo));
                Assert.notNull(userPrizeInfo,"数据错误，userPrizeInfo不可空");
                //12 借款利率折扣卡
                if(userPrizeInfo.getPrizeTypeId()==12){
                    UseInterestDiscountReqDTO requestDTO = new UseInterestDiscountReqDTO();
                    requestDTO.setUserId(project.getBorrowUserId());
                    requestDTO.setPrizeId(project.getPrizeId());
                    requestDTO.setCurrentPeriods(currentPeriod);
                    if (useSceneEnum != null) {
                        requestDTO.setUserPrizeUseSceneEnum(useSceneEnum);
                    }
                    log.info("调用活动借款利率折扣卡接口useInterestDiscountPrize入参，requestDTO={}", JSON.toJSONString(requestDTO));
                    activityPrizeDubboService.useInterestDiscountPrize(requestDTO);
                }
            }catch (Exception e){
                log.error("useInterestDiscountPrize Exception,",e);
            }
        });

    }
}